package org.colin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.colin.dictPojo.SysAppUser;
import org.colin.mapper.SysAppUserMapper;
import org.colin.service.SysAppUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * <p>
 * 系统用户表 服务实现类
 * </p>
 *
 * @author LiRuiFeng
 * @since 2021-01-17
 */
@Service
public class SysAppUserServiceImpl extends ServiceImpl<SysAppUserMapper, SysAppUser> implements SysAppUserService {


    @Autowired
    SysAppUserMapper sysAppUserMapper;

    @Override
    public HashMap getStatistics() {
        HashMap<String, Integer> map = new HashMap<>();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        QueryWrapper<SysAppUser> sysAppUserQueryWrapper = new QueryWrapper<>();
        sysAppUserQueryWrapper.and(i -> i.eq("create_where",2).or().eq("create_where",3));
        Integer allCount = sysAppUserMapper.selectCount(sysAppUserQueryWrapper);
        map.put("allCount",allCount);
        sysAppUserQueryWrapper.ge("create_time",simpleDateFormat.format(new Date()));
        Integer dayCount = sysAppUserMapper.selectCount(sysAppUserQueryWrapper);
        map.put("dayCount",dayCount);
        sysAppUserQueryWrapper = new QueryWrapper<>();
        sysAppUserQueryWrapper.ge("create_time",simpleDateFormat.format(getThisWeekMonday(new Date())));
        sysAppUserQueryWrapper.and(i -> i.eq("create_where",2).or().eq("create_where",3));
        Integer weekCount = sysAppUserMapper.selectCount(sysAppUserQueryWrapper);
        map.put("weekCount",weekCount);
        sysAppUserQueryWrapper = new QueryWrapper<>();
        SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("yyyy-MM-01");
        sysAppUserQueryWrapper.ge("create_time",simpleDateFormat1.format(new Date()));
        sysAppUserQueryWrapper.and(i -> i.eq("create_where",2).or().eq("create_where",3));
        Integer monthCount = sysAppUserMapper.selectCount(sysAppUserQueryWrapper);
        map.put("monthCount",monthCount);
        return map;
    }

    @Override
    public Map getStatisticsByHisAndMonth(Integer year) {
        String startTime = year+"-01-01";
        String endTime = (year+1)+"-01-01";
        Map<String, Integer> hashMap = new TreeMap<>();
        List<HashMap> map = sysAppUserMapper.getStatisticsByHisAndMonth(startTime,endTime);
        hashMap.put(year+"-01",0);
        hashMap.put(year+"-02",0);
        hashMap.put(year+"-03",0);
        hashMap.put(year+"-04",0);
        hashMap.put(year+"-05",0);
        hashMap.put(year+"-06",0);
        hashMap.put(year+"-07",0);
        hashMap.put(year+"-08",0);
        hashMap.put(year+"-09",0);
        hashMap.put(year+"-10",0);
        hashMap.put(year+"-11",0);
        hashMap.put(year+"-12",0);
        for (int i = 0; i < map.size(); i++) {
            HashMap hashMap1 = map.get(i);
            String date =hashMap1.get("date").toString();
            hashMap.put(date,Integer.parseInt(hashMap1.get("count").toString()));
        }
        return hashMap;
    }


    public Date getThisWeekMonday(Date date){
        //testGenerator();
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        // 获得当前日期是一个星期的第几天
        int dayWeek = cal.get(Calendar.DAY_OF_WEEK);
        if (1 == dayWeek) {
            cal.add(Calendar.DAY_OF_MONTH, -1);
        }
        // 设置一个星期的第一天，按中国的习惯一个星期的第一天是星期一
        cal.setFirstDayOfWeek(Calendar.MONDAY);
        // 获得当前日期是一个星期的第几天
        int day = cal.get(Calendar.DAY_OF_WEEK);
        // 根据日历的规则，给当前日期减去星期几与一个星期第一天的差值
        cal.add(Calendar.DATE, cal.getFirstDayOfWeek() - day);
        Date time = cal.getTime();
        return time;
    }
}
